//
//  StrategyViewController.m
//  MyDIYHouse
//
//  Created by dlios on 15-6-29.
//  Copyright (c) 2015年 李平洋. All rights reserved.
//

#import "StrategyViewController.h"
//#import "MJRefresh.h"
#import "StrategyListTableViewCell.h"
#import "UIImageView+WebCache.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "StrategyModel.h"
#import "HTTPTool.h"
#import "StrategyDetailViewController.h"
#include "MJRefresh.h"




@interface StrategyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain)UITableView *strategyTableView;
@property (nonatomic,retain)NSMutableArray *strategyArr;

//几个参数写成属性
@property (nonatomic,assign)NSInteger start;
@property (nonatomic,copy)NSString *count;
@property (nonatomic,copy)NSString *user;
@property (nonatomic,copy)NSString *tag;
@end

@implementation StrategyViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.strategyArr = [NSMutableArray array];
        self.start = 0;
        self.count = @"5";
        self.user = @"(null)";
        self.tag = @"%E5%85%A8%E9%83%A8";
        [self createData];
    }
    return self;
}

#pragma mark 解析数据的方法
- (void)createData
{
    //用拼接字符串的方式,根据参数获取接口的字符串
    NSString *urlStr = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/ideabook?start=%ld&count=%@&user=%@&tag=%@",self.start,self.count,self.user,self.tag];
    
    NSLog(@"%ld",self.start);
    
    //使用封装的AFN解析数据的方法解析数据
    [HTTPTool get:urlStr body:nil httpResult:JSON success:^(id result) {
        NSDictionary *dic = result;
        NSArray *ideabooksArr = dic[@"ideabooks"];
        for (NSDictionary *tempDic in ideabooksArr) {
            StrategyModel *strategy = [[StrategyModel alloc] initWithDic:tempDic];
            //NSLog(@"%@",strategy.bId);
            [self.strategyArr addObject:strategy];
            [strategy release];
        }
        
        [self.strategyTableView reloadData];
        [self.strategyTableView footerEndRefreshing];
    } failure:^(NSError *error) {
        
    }];
    
    
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor brownColor];
    self.title = @"装修攻略";
    
#pragma mark 系统自带的修改title的字体颜色的方法 然并不推荐使用
    UIColor * color = [UIColor whiteColor];
    NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:UITextAttributeTextColor];
    self.navigationController.navigationBar.titleTextAttributes = dict;
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    
    // strategyTableView 的创建
    self.strategyTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64-49) style:0];
    [self.view addSubview:self.strategyTableView];
    self.strategyTableView.dataSource = self;
    self.strategyTableView.delegate = self;
    self.strategyTableView.rowHeight = 160;
    self.strategyTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    // MJRefreshFooter *footer3 = [MJRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerrefereh)];
    
    
    // MJRefreshAutoGifFooter *footer2 = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerrefereh)];
    
    
    //    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerrefereh)];
    //    self.strategyTableView.footer = footer;
    
    //[self.strategyTableView addFooterWithTarget:self action:@selector(footerrefereh)];
    
    [self.strategyTableView addFooterWithCallback:^{
        self.start = self.start+5;
        [self createData];
    }];
    
}

//刷新自定义方法





//strategyTableView 的两个协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // NSLog(@"%ld",self.strategyArr.count);
    return self.strategyArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    StrategyListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[StrategyListTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    
    StrategyModel *strategy = self.strategyArr[indexPath.row];
    
    NSDictionary *tempPicDic = [strategy.photos firstObject];
    NSNumber *picId = [tempPicDic objectForKey:@"id"];
    
    NSNumber *picWidth =[tempPicDic objectForKey:@"width"];
    NSNumber *picHeight =[tempPicDic objectForKey:@"height"];
    
    NSString *picUrl = [NSString stringWithFormat:@"http://gooju.cn/dimages/%@_0_w%@_h%@_m0.jpg",picId,picWidth,picHeight];
    
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:picUrl]];
    cell.titleLabel.text = strategy.title;
    cell.descriptionLabel.text = strategy.bDescription;
    
    NSString *userName = [strategy.user objectForKey:@"userName"];
    cell.userNameLabel.text = userName;
    cell.dateLabel.text = strategy.date;
    
    return cell;
}

#pragma mark 页面跳转方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    StrategyDetailViewController *strategyDetailVC = [[StrategyDetailViewController alloc] init];
    strategyDetailVC.myStrategy = self.strategyArr[indexPath.row];
    
    [self.navigationController pushViewController:strategyDetailVC animated:YES];
    
    
    [strategyDetailVC release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
